Public Class frmManageOther

#Region "Declare Variables"
    Private m_eScreenMode As eScreenMode
    Private m_Other As New PoliceHealthClass.Other
    Private m_ManageOtherControl As New PoliceHealthClass.ManageReferrenceControl
    Private IsPostBack As Boolean = False

    Private cbbStoolExamResultHistory As ComboBox

    Delegate Sub delLoadDatagridviewOther(ByVal intSelectRowIndex As Integer)
#End Region

#Region "Enumuration"
    Private Enum eScreenMode As Integer
        Initial
        View
        Add
        Edit
    End Enum
#End Region

#Region "General Functions"
    Private Sub SetScreenMode(ByVal ScreenMode As eScreenMode)
        m_eScreenMode = ScreenMode
        Select Case ScreenMode
            Case eScreenMode.Initial
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, btnDelete, btnEdit, txtOtherNameTha, txtOtherNameEng)
                PoliceHealthClass.Utilities.EnableControl(True, btnAdd)

                dgvOther.Enabled = True

                If Not Me.IsPostBack Then

                    ConnectionString.ShowLoadingForm()
                    Me.Invoke(New delLoadDatagridviewOther(AddressOf LoadDatagridviewOther), 0)
                    ConnectionString.TerminateLoadingForm()

                    'LoadDatagridviewOther()
                    Me.IsPostBack = True
                End If

            Case eScreenMode.View
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, txtOtherNameTha, txtOtherNameEng)
                PoliceHealthClass.Utilities.EnableControl(True, btnEdit, btnDelete, btnAdd)

                dgvOther.Enabled = True
            Case eScreenMode.Add
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtOtherNameTha, txtOtherNameEng)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)
                ClearFormData()

                dgvOther.Enabled = False

            Case eScreenMode.Edit
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtOtherNameTha, txtOtherNameEng)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)

                dgvOther.Enabled = False
        End Select


    End Sub


    Private Function VerifyInputData() As Boolean
        Return m_ManageOtherControl.verifyInsert()
    End Function

    Private Sub LoadDatagridviewOther(Optional ByVal intSelectRowIndex As Integer = 0)
        dgvOther.Enabled = True
        m_Other.LoadOther()

        Dim intRowCount As Integer = 0
        dgvOther.Rows.Clear()
        While m_Other.NextRecord
            dgvOther.Rows.Add()
            dgvOther.Rows(intRowCount).Cells("Order").Value = intRowCount + 1
            dgvOther.Rows(intRowCount).Cells("OtherID").Value = m_Other.other_id
            dgvOther.Rows(intRowCount).Cells("OtherNameThai").Value = m_Other.other_name_tha
            dgvOther.Rows(intRowCount).Cells("OtherNameEng").Value = m_Other.other_name_eng


            dgvOther.Rows(intRowCount).Selected = False

            intRowCount += 1
        End While

        If intSelectRowIndex < dgvOther.Rows.Count Then
            dgvOther.Rows(intSelectRowIndex).Selected = True
            dgvOther.CurrentCell = dgvOther.Rows(intSelectRowIndex).Cells(0)
        End If


        If dgvOther.Rows.Count > 0 Then
            SetScreenMode(eScreenMode.View)
            LoadDataForm(dgvOther.Rows(intSelectRowIndex).Cells("OtherID").Value)
        Else
            If Me.IsPostBack Then
                SetScreenMode(eScreenMode.Initial)
            End If
            ClearFormData()
        End If
    End Sub

    Private Sub LoadDataForm(ByVal OtherID As String)
        txtOtherID.Text = OtherID

        'Load Form
        If dgvOther.SelectedRows.Count > 0 Then
            txtOtherNameTha.Text = dgvOther.SelectedRows(0).Cells("OtherNameThai").Value
            txtOtherNameEng.Text = dgvOther.SelectedRows(0).Cells("OtherNameEng").Value
        Else
            txtOtherNameTha.Text = dgvOther.Rows(0).Cells("OtherNameThai").Value
            txtOtherNameEng.Text = dgvOther.Rows(0).Cells("OtherNameEng").Value
        End If

    End Sub

    Private Sub ClearFormData()
        txtOtherNameTha.Text = ""
        txtOtherNameEng.Text = ""
        PoliceHealthClass.Utilities.EnableControl(False, btnEdit)
    End Sub






#End Region

#Region "Event Handling Functions"
    Private Sub frmPolice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CMU_KPI_PHASE_1DataSet.tbError_Logging' table. You can move, or remove it, as needed.
        SetScreenMode(eScreenMode.Initial)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If dgvOther.SelectedRows.Count > 0 Then
            LoadDataForm(dgvOther.Rows(0).Cells("OtherID").Value)
            SetScreenMode(eScreenMode.View)
        Else
            SetScreenMode(eScreenMode.Initial)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim SelectIndex As Integer = 0
        Dim blVerifyInputControl As Boolean = False


        If m_eScreenMode = eScreenMode.Edit OrElse m_eScreenMode = eScreenMode.Add Then
            If VerifyInputData() Then

                If dgvOther.SelectedRows.Count > 0 Then
                    SelectIndex = dgvOther.SelectedRows(0).Index
                End If


                m_Other.Other_id = PoliceHealthClass.Utilities.ClearEmptyToMinValue(txtOtherID.Text, TypeCode.Int32)
                m_Other.Other_name_tha = txtOtherNameTha.Text
                m_Other.Other_name_eng = txtOtherNameEng.Text



                If m_eScreenMode = eScreenMode.Edit Then
                    m_ManageOtherControl.showInsertResult(m_Other.updateOther(m_Other))
                ElseIf m_eScreenMode = eScreenMode.Add Then
                    m_ManageOtherControl.showInsertResult(m_Other.insertOther(m_Other))
                End If


                ConnectionString.ShowLoadingForm()
                Me.Invoke(New delLoadDatagridviewOther(AddressOf LoadDatagridviewOther), dgvOther.SelectedRows(0).Index)
                ConnectionString.TerminateLoadingForm()

                'LoadDatagridviewOther(dgvOther.SelectedRows(0).Index)

            End If
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        SetScreenMode(eScreenMode.Add)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        SetScreenMode(eScreenMode.Edit)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If m_ManageOtherControl.verifyDelete() Then
            m_Other.Other_id = txtOtherID.Text
            m_ManageOtherControl.showDeleteResult(m_Other.deleteOther(m_Other))

            ConnectionString.ShowLoadingForm()
            Me.Invoke(New delLoadDatagridviewOther(AddressOf LoadDatagridviewOther), 0)
            ConnectionString.TerminateLoadingForm()

            'LoadDatagridviewOther()
        End If
    End Sub

    Private Sub dgvOther_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvOther.RowEnter
        LoadDataForm(dgvOther.Rows(e.RowIndex).Cells("OtherID").Value)
    End Sub
#End Region

End Class